package com.yzf.wisecoder.wisecoderservice.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzf.uidgeneratorcore.impl.CachedUidGenerator;
import com.yzf.wisecoder.wisecodercommon.constant.Constant;
import com.yzf.wisecoder.wisecodercommon.exception.RRException;
import com.yzf.wisecoder.wisecodercommon.response.Query;
import com.yzf.wisecoder.wisecodercommon.utils.SecurityUtils;
import com.yzf.wisecoder.wisecoderservice.service.RoleService;
import com.yzf.wisecoder.wisecoderstorage.mysql.dao.RoleDao;
import com.yzf.wisecoder.wisecoderstorage.mysql.entity.RoleEntity;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author jianghuiyong
 * @email jianghuiyong@yunzhangfang.com
 * @date 2020-05-06 17:43:12
 */
@Service("roleService")
@AllArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleDao, RoleEntity> implements RoleService {

    private final CachedUidGenerator cachedUidGenerator;

    @Override
    public IPage<RoleEntity> queryPage(Map<String, Object> params) {
        String roleName = (String) params.get("roleName");
        IPage<RoleEntity> page = page(
                new Query<RoleEntity>(params).getPage(),
                new QueryWrapper<RoleEntity>().eq("deleted", Boolean.FALSE)
                        .eq("tenant_id", SecurityUtils.getTenantId())
                        .like("role_name", roleName)
        );

        return page;
    }

    @Override
    public List<RoleEntity> queryList(Map<String, Object> params) {
        String roleName = (String) params.get("roleName");
        List<RoleEntity> list = list(new QueryWrapper<RoleEntity>().eq("deleted", Boolean.FALSE)
                .eq("tenant_id", SecurityUtils.getTenantId())
                .like("role_name", roleName));
        return list;
    }

    @Override
    public boolean saveOrUpdateRole(RoleEntity roleEntity) {
        RoleEntity roleEntity1 = getOne(new QueryWrapper<RoleEntity>()
                .eq("role_name", roleEntity.getRoleName())
                .eq("tenant_id", SecurityUtils.getTenantId()));
        if (roleEntity1 != null && (roleEntity.getRoleId() == null
                || roleEntity.getRoleId().longValue() != roleEntity1.getRoleId().longValue())) {
            throw new RRException("角色名称不能重复");
        }
        if (roleEntity.getRoleId() == null) {
            roleEntity.setCreator(SecurityUtils.getUserId())
                    .setCreateTime(LocalDateTime.now())
                    .setTenantId(SecurityUtils.getTenantId())
                    .setRoleId(cachedUidGenerator.getUID());

        }
        saveOrUpdate(roleEntity);
        return true;
    }

    @Override
    public List<RoleEntity> queryUserRoles(Long userId, Long tenantId) {
        return baseMapper.queryUserRoles(userId, tenantId);
    }

    @Override
    public boolean hasAdmin(Long userId, Long tenantId) {
        List<RoleEntity> list = queryUserRoles(userId, tenantId);
        if (CollectionUtil.isEmpty(list)) {
            return false;
        } else {
            return list.stream().anyMatch(roleEntity -> Constant.ADMIN.equals(roleEntity.getRoleCode()));
        }
    }

}